const db = wx.cloud.database()
let idx = -1,
    word = '',
    cur_date = new Date,
    mon = [],
    day = [],
    word_date = '',
    starData = {},
    origin_data = '',
    myaudio = wx.createInnerAudioContext({
        useWebAudioImplement: true
    });

Page({

    /**
     * 页面的初始数据
     */
    data: {
        tipNot: false,
        tip_word: '',
        inputValue: '',
        definition: ''
    },

    bindKeyInput: function (e) {
        this.setData({
            inputValue: e.detail.value
        })
    },

    async onLoad(options) {
        console.log("当前idx:",idx)
        word_date = JSON.parse(options.date)
        origin_data = JSON.parse(options.origin_data)
        console.log('origin_data', origin_data)
        mon = word_date.map(item => {
            return item.mon
        })
        day = word_date.map(item => {
            return item.day
        })
        console.log('mon', mon, 'day', day)
        console.log('要删除的数据', word_date)
        this.next()
        console.log(idx)
    },

    // onShow(){
    //     this.onLoad()
    // },

    tip: function () {
        this.setData({
            tipNot: !this.data.tipNot,
            tip_word: word
        })
        console.log('点击了提示', this.data.tip_word, this.data.tipNot)
    },

    async next() {
        let {
            inputValue
        } = this.data
        console.log(inputValue == word)
        if (inputValue == word) {
            idx += 1
            let wordList = wx.getStorageSync('selectData')
            if (idx <= wordList.length - 1) {
                word = wordList[idx]
                console.log(word)
                let res = await wx.cloud.callFunction({
                    name: 'queryWord',
                    data: {
                        content: word
                    }
                })
                console.log('res', res)
                this.setData({
                    inputValue: '',
                    tipNot: false,
                    definition: res.result[0].definition
                })
                console.log(res.result[0].definition)
            } else {
                this.learnOver()
            }
        } else {
            wx.showToast({
                title: '拼写有误',
                icon: 'none'
            })
        }
    },

    async learnOver(){ // 复习完成
        await wx.showModal({
            title: '复习完成啦！',
            showCancel: false,
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        mask: true
                    })
                    starData = wx.getStorageSync('starList')
                    console.log(starData)
                    wx.cloud.callFunction({
                        name: 'addDictWord', //云函数名
                        data: {
                            type: 'addmaster',
                            openid: wx.getStorageSync('userInfo').openid,
                            year: cur_date.getFullYear(),
                            month: cur_date.getMonth() + 1, //月  
                            day: cur_date.getDate(), //日  
                            origin_data: origin_data
                        }
                    }).then(res => {
                        console.log('数组写入成功：', res)
                        new Promise(() =>{ // 删除复习本的
                            wx.cloud.callFunction({
                                name: 'removeDictWord',
                                data: {
                                    type:"rvewList",
                                    year: cur_date.getFullYear(),
                                    mon: mon,
                                    day: day
                                }
                            }).then(
                                wx.hideLoading({}),
                                wx.redirectTo({
                                    url: '/pages/wordBook/masterWordList/masterWordList',
                                })
                            )
                        })
                    }).catch(res => {
                        console.log('数组写入失败：', res)
                    })
                }
            }
        })
    },

    // 单词发音
    read: function () {
        myaudio = wx.createInnerAudioContext({
            useWebAudioImplement: true
        });

        myaudio.src = 'https://dict.youdao.com/dictvoice?audio=' + word + '&type=2'
        myaudio.play();
    },

    back: function () {
        wx.showModal({
            title: '提示',
            content: '退出不会保留当前进度哦',
            success(res) {
                if (res.confirm) {
                    wx.navigateTo({
                        url: '/pages/study/reviewWord/reviewWord',
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },

    async onUnload() { // 退出当前页面者退出缓存
        idx = -1,
        word = '',
        mon = [],
        day = [],
        word_date = '',
        starData = {},
        origin_data = '',
        console.log(origin_data,mon,day)
        wx.removeStorage({
            key: 'selectData',
            success(res) {
                console.log('已清除selectData')
            }
        })
    }

})